//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BrownianGenerator.h"
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (boost::shared_ptr<QuantLib::BrownianGenerator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGenerator = NULL;
#endif
	_ppBrownianGenerator = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (QuantLib::BrownianGenerator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBrownianGenerator = NULL;
#endif
	_ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (&childNative);
    
    _BrownianGeneratorOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (CBrownianGenerator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGenerator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGenerator = NULL;
#endif
	if (!t->IsSubclassOf(CBrownianGenerator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (QuantLib::Handle<QuantLib::BrownianGenerator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianGenerator = &childNative;
	_ppBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::BrownianGenerator>>(childNative.currentLink());
    
    _BrownianGeneratorOwner = owner;
}
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (QuantLib::Handle<QuantLib::BrownianGenerator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianGenerator = &childNative;
	_ppBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::BrownianGenerator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CBrownianGenerator::CBrownianGenerator (QuantLib::BrownianGenerator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianGenerator = NULL;
#endif
	_ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (new QuantLib::BrownianGenerator (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CBrownianGenerator::~CBrownianGenerator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBrownianGenerator != NULL)
    {
	    delete _ppBrownianGenerator;
        _ppBrownianGenerator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CBrownianGenerator::!CBrownianGenerator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBrownianGenerator != NULL)
    {
	    delete _ppBrownianGenerator;
    }
}
QuantLib::BrownianGenerator& Cephei::QL::Models::Marketmodels::CBrownianGenerator::GetReference ()
{
    if (_ppBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return **_ppBrownianGenerator;
}
boost::shared_ptr<QuantLib::BrownianGenerator>& Cephei::QL::Models::Marketmodels::CBrownianGenerator::GetShared ()
{
    if (_ppBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return *_ppBrownianGenerator;
}
QuantLib::BrownianGenerator* Cephei::QL::Models::Marketmodels::CBrownianGenerator::GetPointer ()
{
    if (_ppBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return &**_ppBrownianGenerator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BrownianGenerator>& Cephei::QL::Models::Marketmodels::CBrownianGenerator::GetHandle ()
{
	if (_phBrownianGenerator == NULL)
	{
		_phBrownianGenerator = new Handle<QuantLib::BrownianGenerator> (*_ppBrownianGenerator);
	}
	return *_phBrownianGenerator;
}
#endif
bool Cephei::QL::Models::Marketmodels::CBrownianGenerator::HasNative () 
{
	return (_ppBrownianGenerator != NULL);
}

Double Cephei::QL::Models::Marketmodels::CBrownianGenerator::NextPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBrownianGenerator)->nextPath ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::CBrownianGenerator::NextStep (Cephei::IVector<Double>^ prm1)
{
    CoVector<Double>^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CoVector<Double>^> (prm1);
        _Cprm1->Lock();
        INativeVector<Double>^ _NCIprm1 = _Cprm1->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprm1 = safe_cast<CDoubleVector^>(_NCIprm1);
        std::vector<QuantLib::Real>& _prm1 = static_cast<std::vector<QuantLib::Real>&> (_NCprm1->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBrownianGenerator)->nextStep ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CBrownianGenerator::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBrownianGenerator)->numberOfFactors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CBrownianGenerator::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBrownianGenerator)->numberOfSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

